package com.xc.sys.controller;

import com.xc.common.object.LayuiTableResponse;
import com.xc.common.object.PageInfo;
import com.xc.common.object.ResponseEntity;
import com.xc.common.tool.Helper;
import com.xc.db.tables.BaseCodeNumber;
import com.xc.sys.entity.BaseCodeNumberEntity;
import com.xc.sys.entity.SysOrganizationEntity;
import com.xc.sys.entity.SysUserEntity;
import com.xc.sys.service.BaseCodeNumberService;
import com.xc.sys.service.SysOrganizationService;
import com.xc.sys.vo.BaseCodeNumberVO;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping({ "/baseCodeNumber" })
public class BaseCodeNumberController {
	@Autowired
	private BaseCodeNumberService baseCodeNumberService;
	private BaseCodeNumber BASECODENUMBER = BaseCodeNumber.BASE_CODE_NUMBER.as("BASECODENUMBER");
	@RequestMapping({ "/getPageList" })
	public LayuiTableResponse<BaseCodeNumberVO> getPageList(BaseCodeNumberEntity entity, Integer limit, Integer page,
			HttpServletRequest request) {
		LayuiTableResponse<BaseCodeNumberVO> ltr = new LayuiTableResponse<>();
		Condition cond = BASECODENUMBER.ID.isNotNull();
		String prefix = request.getParameter("prefix");
		if(StringUtils.isNotEmpty(prefix)) {
			cond = BASECODENUMBER.PREFIX.eq(prefix);
		}
		Long count = baseCodeNumberService.count(cond);
		PageInfo<BaseCodeNumberEntity> pageInfo = new PageInfo<>(limit, page);
		ltr.setCount(count);
		Result<Record> result = baseCodeNumberService.select(cond, pageInfo, BASECODENUMBER.PREFIX.asc());
		List<BaseCodeNumberVO> list = result.into(BaseCodeNumberVO.class);
		for(BaseCodeNumberVO bt : list){
			SysOrganizationEntity org = SysOrganizationService.getFromCaches(bt.getOrgId());
			bt.setOrgName(org==null?"":org.getName());
		}
		ltr.setData(list);
		return ltr;
	}
	@RequestMapping({ "/getById" })
	public BaseCodeNumberEntity getById(String id) {
		BaseCodeNumberEntity entity = baseCodeNumberService.getEntityById(id);
		return entity;
	}

	@RequestMapping({ "/save" })
	public ResponseEntity<BaseCodeNumberEntity> save(@RequestBody BaseCodeNumberEntity entity,HttpServletRequest request) {
		ResponseEntity<BaseCodeNumberEntity> res = new ResponseEntity<>();
		SysUserEntity me = Helper.getMe(request);
		res.setCode(0);
		if(entity.getId()==null||entity.getId().equals("")) {
			entity.setOrgId(me.getOrgId());
		}
		baseCodeNumberService.save(entity);
		return res;
	}

	@RequestMapping({ "/delete" })
	public LayuiTableResponse<BaseCodeNumberEntity> delete(@RequestParam("ids[]") String[] ids) {
		LayuiTableResponse<BaseCodeNumberEntity> ltr = new LayuiTableResponse<>();
		for (String id : ids) {
			baseCodeNumberService.deleteById(id);
		}
		return ltr;
	}
}
